package elements;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import shapes.*;

public class Viewport extends Canvas {

	Graphics2D gr;
	final int larg = 525/2, alt = 525/2, wlarg = 505/2, walt = 505/2;
	double zoom = 1;
	Window window;
	DisplayFile df;
	
	public Viewport(Window w) 
	{ 
	 	window = w;
	 	this.setBackground(Color.WHITE);
	}
	
	public void refresh(){
		window.refresh();
	}
	
    @Override
	public void paint(Graphics g) 
	{
		gr = (Graphics2D) g;
		gr.setColor(Color.BLACK);
		gr.drawRect(10,10,505,505);
		gr.setColor(Color.GRAY);
		gr.drawRect(0,0,524,524);
		
		for (Shape obj : window.clipshapes.values()) {
			switch (obj.type) {
			case 0:
				int x = (int) (wlarg * obj.ptsn[0][0] + larg);
				int y = (int) (alt - (walt * obj.ptsn[1][0]));
				gr.setColor(obj.color);
				
				gr.drawLine(x, y, x, y);
				break;
			case 1:
				int x1 = (int) (wlarg * obj.ptsn[0][0] + larg);
				int y1 = (int) (alt - (walt * obj.ptsn[1][0]));
				int x2 = (int) (wlarg * obj.ptsn[0][1] + larg);
				int y2 = (int) (alt - (walt * obj.ptsn[1][1]));
				gr.setColor(obj.color);
				gr.drawLine(x1, y1, x2, y2);
				break;
			case 2:
				int[][] pts = new int[2][obj.ptsn[0].length];

				for (int i = 0; i < obj.ptsn[0].length; i++) {
					pts[0][i] = (int) (wlarg * obj.ptsn[0][i] + larg);
					pts[1][i] = (int) (alt - (walt * obj.ptsn[1][i]));
				}
				gr.setColor(obj.color);
				if(obj.solid)
					gr.fillPolygon(pts[0], pts[1], pts[0].length);
				else
					gr.drawPolygon(pts[0], pts[1], pts[0].length);
				
				break;
			case 3:
				pts = new int[2][obj.ptsn[0].length];
				for (int i = 0; i < obj.ptsn[0].length; i++) {
					pts[0][i] = (int) (wlarg * obj.ptsn[0][i] + larg);
					pts[1][i] = (int) (alt - (walt * obj.ptsn[1][i]));
				}
				gr.setColor(obj.color);
				gr.drawPolyline(pts[0], pts[1], pts[0].length);
			}
		}
		
	}

}

